//格式化时间
//dateTime：时间戳
function getFormtTime(dateTime,flag) {
    if(dateTime != null ) {
        //若传入的dateTime为字符串类型，需要进行转换成数值，若不是无需下面注释代码
        var time = parseInt(dateTime)
        var date = new Date(time);
        //获取年份
        var YY = date.getFullYear();
        //获取月份
        var MM = (date.getMonth() + 1 < 10 ? '0'+(date.getMonth() + 1) : date.getMonth() + 1);
        //获取日期
        var DD = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate());
        if(flag) { //flag为true，显示时分秒格式
            //获取小时
            var hh = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours());
            //获取分
            var mm = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes());
            ///获取秒
            var ss = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
            //返回时间格式： 2020-11-09 13:14:52
            return YY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss;
        } else {
            //返回时间格式： 2020-11-09
            return YY + '-' + MM + '-' + DD;
        }
    } else {
        return "";
    }
}
$.ajax({
    type:"post",
    url:"/market/queryAllMarket",
    data:{},
    dataType:"json",
    success:function(r) {
        if (r.data != null) {
            var t = "<tr>\n" +
                "          <th>序号</th>\n" +
                "          <th>客户姓名</th>\n" +
                "          <th>性别</th>\n" +
                "          <th>联系电话</th>\n" +
                "          <th>所属公司</th>\n" +
                "          <th>所属区域</th>\n" +
                "          <th>分配时间</th>\n" +
                "          <th>客户人员</th>\n" +
                "          <th>操作</th>\n" +
                "        </tr>";
            for (var i = 0; i < r.data.length; i++) {
                t += "<tr> <td>" + r.data[i].customid + "</td><td>" + r.data[i].customname + "</td><td>" + r.data[i].sex + "</td><td>" +r.data[i].telephone+ "</td><td>"+r.data[i].company+"</td><td>"+r.data[i].provin.p_name+"</td><td>"+getFormtTime(r.data[i].distractime,false)+"</td><td>"+r.data[i].users2.uname+"</td><td>\n" +
                    "          \t<a href=\"javascript:void(0)\" class=\"tablelink\">查看详情</a>\n" +
                    "          </td></tr>"
            }
            $(".tablelist").html(t)
            // var pagenum = document.getElementById("pagenum");
            $("#pagenum").html(r.pageNum)
            // var totalrows = document.getElementById("totalrows");
            // totalrows.innerHTML=r.totalRows
            $("#totalrows").html(r.totalRows)

            $(".tablelink").click(function(){

                var text = $(this).text();
                var customid = $(this).parents("tr")[0].children[0].innerHTML
                if (text=="查看详情"){
                    window.location.href="/market/queryOneCustom/"+ customid
                }
            });


            $("#shangyiye").click(function () {
                if (pagenum.innerHTML>1){
                    pagenum.innerHTML=Number(pagenum.innerHTML)-1;
                }else{
                    alert("已经是第一页了")
                }

                var n=pagenum.innerHTML
                if (n>=1){
                    $.ajax({
                        type:"post",
                        url:"/market/queryAllMarket",
                        data:{pageNum:n},
                        dataType:"json",
                        success:function(r){
                            if (r.data != null){
                                var t = "<tr>\n" +
                                    "          <th>序号</th>\n" +
                                    "          <th>客户姓名</th>\n" +
                                    "          <th>性别</th>\n" +
                                    "          <th>联系电话</th>\n" +
                                    "          <th>所属公司</th>\n" +
                                    "          <th>所属区域</th>\n" +
                                    "          <th>分配时间</th>\n" +
                                    "          <th>客户人员</th>\n" +
                                    "          <th>操作</th>\n" +
                                    "        </tr>";
                                for (var i = 0; i < r.data.length; i++) {
                                    t += "<tr> <td>" + r.data[i].customid + "</td><td>" + r.data[i].customname + "</td><td>" + r.data[i].sex + "</td><td>" +r.data[i].telephone+ "</td><td>"+r.data[i].company+"</td><td>"+r.data[i].provin.p_name+"</td><td>"+getFormtTime(r.data[i].distractime,false)+"</td><td>"+r.data[i].users2.uname+"</td><td>\n" +
                                        "          \t<a href=\"javascript:void(0)\" class=\"tablelink\">查看详情</a>\n" +
                                        "          </td></tr>"
                                }
                                $(".tablelist").html(t)
                                // var pagenum = document.getElementById("pagenum");
                                $("#pagenum").html(r.pageNum)
                                // var totalrows = document.getElementById("totalrows");
                                // totalrows.innerHTML=r.totalRows
                                $("#totalrows").html(r.totalRows)

                                $(".tablelink").click(function(){

                                    var text = $(this).text();
                                    var customid = $(this).parents("tr")[0].children[0].innerHTML
                                    if (text=="查看详情"){
                                        window.location.href="/market/queryOneCustom/"+ customid
                                    }
                                });


                            }
                        },
                        error:function (r) {
                            alert("!!!")
                        }

                    })
                }else{
                    alert("已经是第一页了")
                }


            })


            $("#xiayiye").click(function () {
                if (pagenum.innerHTML<r.totalPage){
                    pagenum.innerHTML=Number(pagenum.innerHTML)+1
                }else{
                    alert("已经是最后一页了")
                }

                var n=pagenum.innerHTML
                if (n<=r.totalPage){
                    $.ajax({
                        type:"post",
                        url:"/market/queryAllMarket",
                        data:{pageNum:n},
                        dataType:"json",
                        success:function(r){
                            if (r.data != null){
                                var t = "<tr>\n" +
                                    "          <th>序号</th>\n" +
                                    "          <th>客户姓名</th>\n" +
                                    "          <th>性别</th>\n" +
                                    "          <th>联系电话</th>\n" +
                                    "          <th>所属公司</th>\n" +
                                    "          <th>所属区域</th>\n" +
                                    "          <th>分配时间</th>\n" +
                                    "          <th>客户人员</th>\n" +
                                    "          <th>操作</th>\n" +
                                    "        </tr>";
                                for (var i = 0; i < r.data.length; i++) {
                                    t += "<tr> <td>" + r.data[i].customid + "</td><td>" + r.data[i].customname + "</td><td>" + r.data[i].sex + "</td><td>" +r.data[i].telephone+ "</td><td>"+r.data[i].company+"</td><td>"+r.data[i].provin.p_name+"</td><td>"+getFormtTime(r.data[i].distractime,false)+"</td><td>"+r.data[i].users2.uname+"</td><td>\n" +
                                        "          \t<a href=\"javascript:void(0)\" class=\"tablelink\">查看详情</a>\n" +
                                        "          </td></tr>"
                                }
                                $(".tablelist").html(t)
                                // var pagenum = document.getElementById("pagenum");
                                $("#pagenum").html(r.pageNum)
                                // var totalrows = document.getElementById("totalrows");
                                // totalrows.innerHTML=r.totalRows
                                $("#totalrows").html(r.totalRows)

                                $(".tablelink").click(function(){

                                    var text = $(this).text();
                                    var customid = $(this).parents("tr")[0].children[0].innerHTML
                                    if (text=="查看详情"){
                                        window.location.href="/market/queryOneCustom/"+ customid
                                    }
                                });

                            }
                        },
                        error:function (r) {
                            alert("!!!")
                        }

                    })
                }else{
                    alert("已经是最后一页了")
                }


            })


        }
    }
})

$("#check").click(function () {
    var companyName = $("#companyName").val();
    var customName = $("#customName").val();
    var province = $("#province option:selected").text();
    var address1;
    if (province=="江西"){
        address1="1"
    }else if(province=="广东"){
        address1="2"
    }else if(province=="江苏"){
        address1="3"
    }else if(province=="安徽"){
        address1="4"
    }else if(province=="浙江"){
        address1="5"
    }
    $.ajax({
        type: "post",
        url: "/market/queryAllCustomByCondition",
        data: {company:companyName,customname:customName,address:address1},
        dataType: "json",
        success: function (r) {
            if(r!="null"){
                var t = "<tr>\n" +
                    "          <th>序号</th>\n" +
                    "          <th>客户姓名</th>\n" +
                    "          <th>性别</th>\n" +
                    "          <th>联系电话</th>\n" +
                    "          <th>所属公司</th>\n" +
                    "          <th>所属区域</th>\n" +
                    "          <th>分配时间</th>\n" +
                    "          <th>客户人员</th>\n" +
                    "          <th>操作</th>\n" +
                    "        </tr>";
                for (var i = 0; i < r.length; i++) {
                    t += "<tr> <td>" + r[i].customid + "</td><td>" + r[i].customname + "</td><td>" + r[i].sex + "</td><td>" +r[i].telephone+ "</td><td>"+r[i].company+"</td><td>"+r[i].provin.p_name+"</td><td>"+getFormtTime(r[i].distractime,false)+"</td><td>"+r[i].users2.uname+"</td><td>\n" +
                        "          \t<a href=\"javascript:void(0)\" class=\"tablelink\">查看详情</a>\n" +
                        "          </td></tr>"
                }
                $(".tablelist").html(t)
                // var pagenum = document.getElementById("pagenum");
                $("#pagenum").html(r.pageNum)
                // var totalrows = document.getElementById("totalrows");
                // totalrows.innerHTML=r.totalRows
                $("#totalrows").html(r.totalRows)

                $(".tablelink").click(function(){

                    var text = $(this).text();
                    var customid = $(this).parents("tr")[0].children[0].innerHTML
                    if (text=="查看详情"){
                        window.location.href="/market/queryOneCustom/"+ customid
                    }
                });

            }else {
                alert("没有查询结果！")
                location.href="customerBrowse.html"
            }
        }
    })
})


